const liveDao = require('../dao/liveDao')
const jwt = require('jsonwebtoken')
const config = require('../config/config.js')
const utils = require('../utils/utils')

// 添加
exports.start = async ctx => {
  let body = ctx.request.body
  let token = ctx.request.headers["authorization"];
  let payload = jwt.verify(token,config.security.secretKey)
  body.uid = payload.id
  body.status = 1
  body.code = utils.randomID().substring(0,5);
  // rtmp暂且这样设置
  body.rtmp = 'rtmp://192.168.0.103/live'
  console.log(body);
  const res = await liveDao.create(body);
  ctx.success('创建成功',res)
}

// 获取全部
exports.getAll = async ctx => {
  let { p = 1,limit = 999 } = ctx.request.query
  let offset = (p - 1) * limit;
  let body = {
    offset:offset,
    limit:Number(limit)
  }
  const res = await liveDao.findAll(body);
  ctx.success('',res)
}

// 获取详情
exports.getDetail = async ctx => {
  let { id } = ctx.request.query
  const res = await liveDao.findById(id);
  ctx.success('',res)
}

// 删除某条数据
exports.del = async ctx => {
  let { id } = ctx.request.query
  const res = await liveDao.delById(id);
  ctx.success('删除成功')
}

// 修改某条数据
exports.upd = async ctx => {
  let body = ctx.request.body
  const res = await liveDao.upd(body);
  ctx.success('修改成功')
}
